using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.NamingConventions;
using System.Data;

namespace Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel
{
    public static class IdentityHelper
    {

        private static string connectionSettingProvider = string.Empty;

        public static string ConnectionSettingProvider
        {
            get
            {
                return connectionSettingProvider;
            }
            set
            {
                connectionSettingProvider = value;
            }
        }

        public static string GetIdentityConstructor(Identity identity, bool variablesNamesOnly, bool removeField)
        {
            string response = "";
            
            if (identity != null)
            {
                foreach (PropertyParameterMapping parameterMapping in identity.ParameterMappings)
                {
                    if (parameterMapping.ParameterInstance.Direction == ParameterDirection.Input || parameterMapping.ParameterInstance.Direction == ParameterDirection.InputOutput)
                    {
                        if (response.Length > 0)
                        {
                            response += ", ";
                        }

                        if (variablesNamesOnly)
                        {
                            response += GetMethodParameterName(parameterMapping.Property);
                        }
                        else
                        {
                            string propertyType = parameterMapping.ParameterInstance.DbDataType.Type.ToString();
                            response += propertyType + " " + GetMethodParameterName(parameterMapping.Property);
                        }
                    }
                }

            }
            return response;

        }

        public static string GetIdentityConstructor(Identity identity, bool variablesNamesOnly)
        {
            return GetIdentityConstructor(identity, variablesNamesOnly, false);
        }

        public static bool GenerateIdentityObject(Identity identity)
        {
            // Let's check how many Input or InputOutput parameters there are.  If more than 1, then return true.
            int counter = 0;
            foreach (PropertyParameterMapping item in identity.ParameterMappings)
            {
                if (item.ParameterInstance.Direction == ParameterDirection.Input || item.ParameterInstance.Direction == ParameterDirection.InputOutput) counter++;
                if (counter > 1) return true;
            }
            return false;
        }

        private static string GetVariableName(string type)
        {
            SetNamingStrategy();
            return NamingHelper.GetVariableName(type);
        }

        private static string GetMethodParameterName(string type)
        {
            SetNamingStrategy();
            return NamingHelper.GetMethodParameterName(type);
        }

        private static void SetNamingStrategy()
        {
            NamingHelper.SetNamingStrategy(ConnectionSettingProvider);
        }
    }

}
